package iotwisp.school.system.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import iotwisp.school.system.command.CreateFileSCmd;
import iotwisp.school.system.service.FileService;
import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.util.UUIDGenerator;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * {@link diance.system.provider.qo.FileQO}
 * {@link diance.view.system.vo.FileVO}
 * {@link diance.view.system.provider.qo.FileViewQO}
 * {@link FileService}
 * {@link FileSPI}
 * UserFile UserFileQO FileVO FileSPI FileViewSPI FileService FileViewService FileController FileViewController
 * @author yuxx
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
@Table(name = M.TABLE_PREFIX + "file")
@DiscriminatorValue("file")
@SuppressWarnings("serial")
public class File extends StringIdBaseEntity {

        /**
        * 文件名
        */
        @Column(name = "title", length = 128)
        private String title;

        /**
        * 归属主体id
        */
        @Column(name = "owner_id", length = 32)
        private String ownerId;

        /**
        * 归属主体类型
        */
        @Column(name = "owner_type", length = 32)
        private String ownerType;

        /**
        * 可访问相对url
        */
        @Column(name = "url", length = 512)
        private String url;

        /**
        * 文件元数据
        */
        @Column(name = "metadata_json", length = 2048)
        private String metadataJSON;

        /**
        * 创建时间
        */
        @Column(name = "create_date", columnDefinition = M.DATE_COLUMN)
        private Date createDate;

        /**
        * 是否已删除
        */
        @Type(type = "yes_no")
        @Column(name = "remove")
        private Boolean remove;

        /**
        * 类型后缀
        */
        @Column(name = "suffix")
        private String suffix;

        public void create(CreateFileSCmd cmd) {

            setId(UUIDGenerator.getUUID());
            setTitle(cmd.getTitle());
            setOwnerId(cmd.getOwnerId());
            setOwnerType(cmd.getOwnerType());
            setUrl(cmd.getUrl());
            setMetadataJSON(cmd.getMetadataJSON());
            setSuffix(cmd.getSuffix());
            setCreateDate(new Date());
            setRemove(false);
        }

}
